<template>
    <div>
        <el-form :model="searchForm" ref="searchForm" :inline="true">
            <el-form-item>
                <el-input v-model="searchForm.imsi" placeholder="用户识别码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-select v-model="searchForm.smsType" placeholder="运营商类型">
                    <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-date-picker
                        v-model="searchForm.deviceUpTime"
                        type="date"
                        placeholder="上报日期">
                </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button @click="getDeviceList()">搜索</el-button>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="dialogVisible = true">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                border
                stripe
                @selection-change="handleSelectionChange">

            <el-table-column
                    prop="imsi"
                    label="国际用户识别码"
                    width="260">
            </el-table-column>
            <el-table-column
                    prop="smsType"
                    label="运营商类型"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="deviceUpTime"
                    label="设备上报时间"
                    width="180"
                    :formatter="dateFormat">
            </el-table-column>

        </el-table>

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                layout="total, sizes, prev, pager, next, jumper"
                :page-sizes="[10, 20, 50, 100]"
                :current-page="searchForm.current"
                :page-size="searchForm.size"
                :total="total">
        </el-pagination>

        <!--新增对话框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="600px"
                :before-close="handleClose">

            <el-form :model="editForm" ref="editForm" label-width="100px" class="demo-editForm">

                <el-form-item label="用户识别码" prop="imsi" label-width="100px">
                    <el-input v-model="editForm.imsi"></el-input>
                </el-form-item>

                <el-form-item label="运营商类型" prop="smsType" label-width="100px">
                    <el-select v-model="editForm.smsType">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="上报日期" prop="deviceUpTime" label-width="100px">
                    <el-date-picker
                            v-model="editForm.deviceUpTime"
                            type="datetime"
                            placeholder="选择日期时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
                    <el-button @click="resetForm('editForm')">重置</el-button>
                </el-form-item>
            </el-form>

        </el-dialog>
    </div>
</template>

<script>
    import moment from 'moment'
    export default {
        name: "Device",
        data() {
            return {
                options: [{
                    value: 'DX',
                    label: '电信'
                }, {
                    value: 'YD',
                    label: '移动'
                }, {
                    value: 'LT',
                    label: '联通'
                }, {
                    value: 'QT',
                    label: '其它'
                }, {
                    value: 'HW',
                    label: '海外'
                }],
                searchForm: {
                    deviceUpTime: '',
                    size: 10,
                    current: 1
                },
                total: 0,
                tableData: [],
                dialogVisible: false,
                editForm: {}
            }
        },
        created() {
            this.getDeviceList()
        },
        methods: {
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.searchForm.size = val
                this.getDeviceList()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.searchForm.current = val
                this.getDeviceList()
            },
            handleSelectionChange(val) {
                console.log("勾选")
                console.log(val)
            },
            getDeviceList() {
                this.$axios.post("/device/data/list", this.searchForm)
                    .then(res => {
                        this.tableData = res.data.data.records
                        this.searchForm.size = res.data.data.size
                        this.searchForm.current = res.data.data.current
                        this.total = res.data.data.total
                    })
            },
            editHandle(id) {

            },
            setExce(id) {
                console.log('id', id)
                this.$axios.post("/device/data/signHandle/" + id, null).then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        onClose: () => {
                            this.getDeviceList()
                        }
                    });
                })
            },
            handleClose() {
                this.resetForm('editForm')
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.dialogVisible = false
                this.editForm = {}
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/device/data/' + (this.editForm.id?'update' : 'save'), this.editForm)
                            .then(res => {

                                this.$message({
                                    showClose: true,
                                    message: '恭喜你，操作成功',
                                    type: 'success',
                                    onClose:() => {
                                        this.getRoleList()
                                    }
                                });

                                this.dialogVisible = false
                                this.resetForm(formName)
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
             getLocalTime(nS) {
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
             },
            dateFormat(row,column){
                var date = row[column.property];
                if(date === undefined){
                    return ''
                }
                return moment(date).format("YYYY-MM-DD HH:mm:ss")
            }
        }
    }
</script>

<style scoped>
    .el-pagination {
        float: right;
        margin-top: 22px;
    }
</style>